<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Layout
{

    private $_CI;
    public $_langCode = '';
    private $_menu = array();

    function __construct()
    {
        $this->_CI = & get_instance();
        $this->_CI->load->library('Minifier');
        $this->_CI->load->library('Mycommon');
        $this->_langCode = $this->_CI->mycommon->getLangCode();
        $this->_CI->load->language('landing/landing', $this->_langCode);
        $this->_CI->load->model('info/info_model');
//        $this->_initMenu();
    }

    public function _loadHeader($headTitle = '')
    {
        $data = array();
        $this->_CI->config->load('config_info');
        $configInfo = $this->_CI->config->item('infoWebsite');
        $data['headTitle'] = ($headTitle != '') ? $headTitle : $configInfo['headTitle'];
        $data['metaTitle'] = $configInfo['metaTitle'];
        $data['metaKeyword'] = $configInfo['metaKeyword'];
        $data['metaDesc'] = $configInfo['metaDesc'];
        $this->addCssJs();
        $data['combinedCss'] = $this->_CI->minifier->GetCombined('css');
        $data['combinedJs'] = $this->_CI->minifier->GetCombined('js');
        
        $this->_CI->load->library('Blockcache');
        //list Network
        if ($bannerInfo = $this->_CI->blockcache->get('bannerInfo')) {
            
        } else {
            $bannerInfo = array();
            $this->_CI->load->model('option/option_model');
            $bannerInfo = $this->_CI->option_model->getOptionByName('bannerImg');
            
            $this->_CI->blockcache->save('bannerInfo',$bannerInfo,3000);
        }
        $data['bannerInfo'] = $bannerInfo;
        $aInfo = getimagesize(STATIC_DOMAIN.$bannerInfo['value']);
        list($iWidth, $iHeight) = $aInfo;
        $data['iwidth'] = $iWidth;
        $data['iheight'] = $iHeight;
        $this->_CI->load->view('header_view', $data);
    }

    public function _loadMenuBar()
    {
        $data = array();
        $data['lang'] = $this->_CI->lang->line('landing_lang');
        $data['segment'] = $this->_CI->uri->rsegment(1);
        $data['segment_function'] = $this->_CI->uri->rsegment(2);
        $data['menu'] = $this->_menu;
        $data['langCode'] = $this->_langCode;
        
        $this->_CI->load->view('menubar_view', $data);
    }

    private function _initMenu()
    {

        $this->_CI->load->library('Menu');
        $this->_CI->load->library('Blockcache');
        if ($menu = $this->_CI->blockcache->get('menubar')) {
            $this->_menu = $menu;
        } else {
            $this->_CI->load->model('option/option_model');
            $this->_CI->load->model('category/category_model');
            $langCode = $this->_CI->mycommon->fetchLangCode($this->_langCode);
            $lstMenubar = $this->_CI->option_model->getOptionByName('lstMenubar');
            $arrMenubar = explode(',', $lstMenubar['value']);
            $posParent = 0;
            foreach ($arrMenubar as $v) {
                if ($tmpInfo = $this->_CI->category_model->getCategoryInfo($v, $langCode)) {
                    $catInfo = $tmpInfo[0];
                    $path = (($this->_langCode == 'vietnam') ? 'chuyen-muc/' : 'category/') . $this->_CI->mycommon->convertToAlias($catInfo['name']) . '_' . $catInfo['id'];
                    if ($catInfo['href'] == '')
                        $url = site_url($path);
                    else
                        $url = $catInfo['href'];
                    $this->_CI->menu->addMenu($catInfo['id'], $catInfo['name'], $url, $posParent, $catInfo['parent_id']);
                    $posParent++;
                    if ($tmpChild = $this->_CI->category_model->getChildrenCategory($catInfo['id'], $langCode)) {
                        $posChild = 0;
                        foreach ($tmpChild as $v) {
                            $path = (($this->_langCode == 'vietnam') ? 'chuyen-muc/' : 'category/') . $this->_CI->mycommon->convertToAlias($v['name']) . '_' . $v['id'];
                            if ($v['href'] == '')
                                $url = site_url($path);
                            else
                                $url = $v['href'];
                            $this->_CI->menu->addSubmenu($v['parent_id'], $v['id'], $v['name'], $url, $posChild);
                            $posChild++;
                        }
                    }
                }
            }
            $this->_menu = $this->_CI->menu->renderMenu();
            $this->_CI->blockcache->save('menubar', $this->_menu, 3000);
        }
    }

    public function setMenuActive($menuid)
    {
        foreach ($this->_menu as $k => $v) {
            if ($v->id == $menuid) {
                if ($v->parent != 0)
                    $this->setMenuActive($v->parent);
                else
                    $this->_menu[$k]->active = true;
            }
        }
    }

    public function _loadColLeft()
    {
        $data = array();
        $data['lang'] = $this->_CI->lang->line('landing_lang');
        $this->_CI->load->view('colleft_view', $data);
    }
    public function _loadLeft()
    {
        $data = array();
        $this->_CI->load->library('Blockcache');
        //list Network
        if ($listNetwork = $this->_CI->blockcache->get('listNetwork')) {
            
        } else {
            $listNetwork = array();
            $this->_CI->load->model('list_network/list_network_model');
            $listNetwork = $this->_CI->list_network_model->getListNetwork();
            
            $this->_CI->blockcache->save('listNetwork',$listNetwork,3000);
        }
         //list group
        if ($listGroup = $this->_CI->blockcache->get('listGroup')) {
            
        } else {
            $listGroup = array();
            $this->_CI->load->model('list_group/list_group_model');
            $listGroup = $this->_CI->list_group_model->getListGroup();
            
            $this->_CI->blockcache->save('listGroup',$listGroup,3000);
        }
        
        if ($listCity = $this->_CI->blockcache->get('listCity')) {
            
        } else {
            $listCity = array();
            $this->_CI->load->model('list_city/list_city_model');
            $listCity = $this->_CI->list_city_model->getListCity();
            
            $this->_CI->blockcache->save('listCity',$listCity,3000);
        }
        $this->_CI->load->model('item_sim/item_sim_model');
        
        if ($listAds = $this->_CI->blockcache->get('listAds')) {
            
        } else {
            $listAds = array();
            $this->_CI->load->model('ads/ads_model');
            $listAds = $this->_CI->ads_model->getInfoAds(1);
            
            $this->_CI->blockcache->save('listAds',$listAds,3000);
        }

        $this->_CI->load->model('item_sim/item_sim_model');
        $countSimArray = array();
        
        foreach ($listNetwork as $key=>$value) {
        	$countSimArray[$value['name']] = $this->_CI->item_sim_model->getCountListSimByNetworkId($value['id']);
        	
        }
        $data['listAds'] = $listAds;
        $data['listNetwork'] = $listNetwork;
        $data['listGroup'] = $listGroup;
        $data['listCity'] = $listCity;
        $data['countSim'] = $countSimArray;
        $this->_CI->load->view('left_view', $data);
    }

    public function _loadRight()
    {
        
        $data = array();
        $this->_CI->load->library('Blockcache');
        //list Network
        if ($listNetwork = $this->_CI->blockcache->get('listNetwork')) {
            
        } else {
            $listNetwork = array();
            $this->_CI->load->model('list_network/list_network_model');
            $listNetwork = $this->_CI->list_network_model->getListNetwork();
            
            $this->_CI->blockcache->save('listNetwork',$listNetwork,3000);
        }
         //list group
        if ($listGroup = $this->_CI->blockcache->get('listGroup')) {
            
        } else {
            $listGroup = array();
            $this->_CI->load->model('list_group/list_group_model');
            $listGroup = $this->_CI->list_group_model->getListGroup();
            
            $this->_CI->blockcache->save('listGroup',$listGroup,3000);
        }
        
        if ($listCity = $this->_CI->blockcache->get('listCity')) {
            
        } else {
            $listCity = array();
            $this->_CI->load->model('list_city/list_city_model');
            $listCity = $this->_CI->list_city_model->getListCity();
            
            $this->_CI->blockcache->save('listCity',$listCity,3000);
        }
        //list sim giam gia
        if ($listDistCount = $this->_CI->blockcache->get('listDistCount')) {
        
        } else {
        	$listDistCount = array();
        	$this->_CI->load->model('item_sim/item_sim_model');
        	$listDistCount = $this->_CI->item_sim_model->getAllByPrice(1);
        	$this->_CI->blockcache->save('listDistCount',$listDistCount,3000);
        }
        
        //list sim mới.
        if ($listNews = $this->_CI->blockcache->get('listNews')) {
        
        } else {
        	$listNews = array();
        	$this->_CI->load->model('item_sim/item_sim_model');
        	$listNews = $this->_CI->item_sim_model->getAllByPrice(3);
        	$this->_CI->blockcache->save('listNews',$listNews,3000);
        }
        //list nick yahoo and mobile for send
        if ($listContacts = $this->_CI->blockcache->get('listContacts')) {
        
        } else {
        	$listContacts = array();
        	
        	$postId = 'contactInfo';
        	$this->_CI->load->model('option/option_model');
        	$listContacts = ($this->_CI->option_model->getOptionByName($postId));
            $listContacts = json_decode($listContacts['value']);
        	
        	$this->_CI->blockcache->save('listContacts',$listContacts,3000);
        }
        
        //list banner quang cao
        if ($listAdsRight = $this->_CI->blockcache->get('listAdsRight')) {
            
        } else {
            $listAds = array();
            $this->_CI->load->model('ads/ads_model');
            $listAdsRight = $this->_CI->ads_model->getInfoAds(2);
            
            $this->_CI->blockcache->save('listAdsRight',$listAdsRight,3000);
        }
        $yahooArray = array();
        if(isset($listYahoos[0])) {
            $yahooArray = explode(',', $listYahoos[0]['content']);
        }
        
        $data['listAds'] = $listAds;
        $data['listNews'] = $listNews;
        $data['listNetwork'] = $listNetwork;
        $data['listGroup'] = $listGroup;
        $data['listCity'] = $listCity;
        $data['listDistCount'] = $listDistCount;
        $data['listContacts'] = $listContacts;
        $this->_CI->load->view('right_view', $data);
    }
    public function _loadFooter()
    {
        $data = array();
        $this->_CI->load->view('footer_view', $data);
    }
    
    public function _loadSearchForm()
    {
        //session_start();
    	$data = array();
    	$this->_CI->load->library('Blockcache');
    	//list Network
    	if ($listNetwork = $this->_CI->blockcache->get('listNetwork')) {
    	
    	} else {
    		$listNetwork = array();
    		$this->_CI->load->model('list_network/list_network_model');
    		$listNetwork = $this->_CI->list_network_model->getListNetwork();
    	
    		$this->_CI->blockcache->save('listNetwork',$listNetwork,3000);
    	}
    	//list group
    	if ($listGroup = $this->_CI->blockcache->get('listGroup')) {
    	
    	} else {
    		$listGroup = array();
    		$this->_CI->load->model('list_group/list_group_model');
    		$listGroup = $this->_CI->list_group_model->getListGroup();
    	
    		$this->_CI->blockcache->save('listGroup',$listGroup,3000);
    	}
    	
    	$data['list_network'] = $listNetwork;
    	$data['list_group'] = $listGroup;
    	
    	$this->_CI->load->view('search_form_view', $data);
    }

    private function addCssJs()
    {   
        $this->_CI->minifier->AddCss('style');
        $this->_CI->minifier->AddJs('jquery-1.8.2.min');
        $this->_CI->minifier->AddJs('tooltip');
        $this->_CI->minifier->AddJs('ajax');
    }

    public function view($viewFile, $data = array())
    {
        $headTitle = (isset($data['headTitle'])) ? $data['headTitle'] : '';
        $this->_loadHeader($headTitle);
        $this->_loadMenuBar();
        $this->_loadLeft();
        $this->_loadSearchForm();
        $this->_CI->load->view($viewFile, $data);
        $this->_loadRight();
        //$this->_loadColLeft();
        $this->_loadFooter();
    }

}

?>
